//
//  CityViewCell.m
//  We_Travel
//
//  Created by lanou3g on 15/12/10.
//  Copyright © 2015年 lanou3g. All rights reserved.
//

#import "CityViewCell.h"
#import "SiteViewCell.h"
#import "SiteManager.h"
#import "CoreDataToolHelper.h"
#import "Site.h"
#import "url.h"
#import "NH_DataDownloadTools.h"
#import "UIImageView+WebCache.h"

@interface CityViewCell ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,assign) BOOL isNoSite;
@end


@implementation CityViewCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
      
    }
    return self;
}
- (void)setup{
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.scrollEnabled = NO;
    [self.tableView registerNib:[UINib nibWithNibName:@"SiteViewCell" bundle:nil] forCellReuseIdentifier:@"site"];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return [self.arrayData[section] count];

}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.arrayData.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    SiteViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"site"forIndexPath:indexPath];
    Site *site = self.arrayData[indexPath.section][indexPath.row];
    [NH_DataDownloadTools downloadDataWithURL:[NSString stringWithFormat:HT_DetialSiteUrl,[site.siteID integerValue]] andMethod:@"get" andBody:nil andBlock:^(NSData *data) {
        NSDictionary *dictoionary = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
        [cell.imagePhoto sd_setImageWithURL:dictoionary[@"cover"]];
        cell.titleLabel.text = dictoionary[@"name"];
        if ([site.time isEqualToString:@"night"]) {
             cell.timeLabel.text = [NSString stringWithFormat:@"晚上|%@~%@分钟",dictoionary[@"time_consuming_min"],dictoionary[@"time_consuming_max"]];
        }else{
             cell.timeLabel.text = [NSString stringWithFormat:@"白天|%@~%@分钟",dictoionary[@"time_consuming_min"],dictoionary[@"time_consuming_max"]];
        }
    }];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 60;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0,CGRectGetWidth([UIScreen mainScreen].bounds), 30)];
    self.headerLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, 80, headerView.frame.size.height)];
    self.arrangeLabel = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(self.headerLabel.frame)+20, 0,80,30)];
    [headerView addSubview:self.arrangeLabel];
    [headerView addSubview:self.headerLabel];
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(headerView.frame.size.width - 30, 5, 20, 20)];
    imageView.image = [UIImage imageNamed:@"go.png"];
    imageView.tintColor = [UIColor whiteColor];
    [headerView addSubview:imageView];
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0, 0,CGRectGetWidth([UIScreen mainScreen].bounds), 30);
    [headerView addSubview:button];
    button.tag = 200 + section;
    self.headerLabel.text = [NSString  stringWithFormat:@"第%ld天", section+1];
    if ([self.arrayData[section] count] <= 0) {
        self.isNoSite = YES;
        self.arrangeLabel.text = @"未安排";
    }else{
        self.isNoSite = NO;
        self.arrangeLabel.text = @" ";
    }
    [button addTarget:self action:@selector(tapGestureAction:) forControlEvents:UIControlEventTouchUpInside];
    return headerView;
}
- (void)tapGestureAction:(UIButton *)sender{
    self.tapBlock(sender.tag - 200);
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    self.didSelectBlock(indexPath);
}
- (void)awakeFromNib {
    // Initialization code
    [self setup];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}




@end
